﻿using UnityEngine;
using System.Collections;
using ngnclht;

public class GridControl : MonoBehaviour {
	public static string TAG      = "GridControl";
	public Vector2[]     ballPositionList;
	public GameObject[]  ballList;
	public Vector2       gridPos;

	public GameObject    ball;
	// Use this for initialization
	void Start () {
		ballPositionList = new Vector2[81];
		ballList = new GameObject[81];
		for (int i=0; i<81; i++) {
			ballPositionList [i] = new Vector2 (-5, -5);
			ballList [i] = null;
		}
		creatBall ();
		creatBall ();


	}

	// Update is called once per frame
	void Update () {
		if(Input.GetMouseButtonDown(0)){
			detectGridPos();

		}
	}
	//*****
	// detech clicked gridPos
	//*****

	public void detectGridPos(){
		Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
		float xPos = ray.origin.x+Define.widthOfGridSquare/2;
		if(ray.origin.x < 0) xPos =  ray.origin.x-Define.widthOfGridSquare/2;
		int x = (int)(xPos/ (Define.widthOfGridSquare + Define.widthBetweenTwoSquare));
		
		float yPos = ray.origin.y+Define.heightOfGridSquare/2;
		if(ray.origin.y < 0) yPos =  ray.origin.y-Define.heightOfGridSquare/2;
		int y = (int)(yPos/ (Define.heightOfGridSquare + Define.heightBetweenTwoSquare));
		Debug.Log (TAG+"  clicked (x:"+x+" y:"+y+")");

		GameObject ballAtClickPos = findBallAtGridPos (new Vector2(x,y));
		if (ballAtClickPos != null) {
			Animator animator = ballAtClickPos.GetComponent<Animator>();
			animator.SetBool("isClicking",true);
		}

	}

	// tim qua bong o vi tri cu the
	public GameObject findBallAtGridPos(Vector2 gridPos){
		Debug.Log("tim x"+gridPos.x +" y "+gridPos.y);
		for (int i=0; i<81; i++) {
			Vector2 readyExist = ballPositionList[i];
			Debug.Log("duyet x"+readyExist.x +" y "+readyExist.y);
			if(readyExist.x == gridPos.x && readyExist.y == gridPos.y ) return ballList[i];
		}
		return null;
	}

	// tao ngay nhien 3 qua bong
	public void creatBall(){
		int color = 0;
		int emptySlot = countEmptySlot();
		Debug.Log (TAG + " number empty slot is " + emptySlot);


		if(emptySlot >0) for(int i=0;i<3;i++){
			Debug.Log (TAG + " still have empty slots, create ball");
			int x = Random.Range (-5, 5);
			int y = Random.Range (-5, 5);

			if(x >4 || x<-4) x = x/x *4;
			if(y >4 || y<-4) y = y/y *4;


			Vector3 postition = vector3FromGridPos(x,y);	
			Debug.Log(TAG+"slot x "+x+" y "+y+": empty?"+isSlotEmpty(postition));
			int length = arrayRealLength ();
			ballPositionList[length+1] = postition;
			ballList[length+1] =(GameObject) Instantiate(ball,postition,gameObject.transform.localRotation);
		}
		
	}

	// dem xem co bao nhieu 
	public int countEmptySlot(){
		int length = arrayRealLength ();
		if (length == 81)
						return 0;
		return 81 - length;
	}

	// so luong qua bong da duoc tao ra
	public int arrayRealLength(){
		int length = 0;
		for (int i=0; i<81; i++)
			if(ballPositionList [i].x != -5 && ballPositionList [i].y != -5) length++;
		return length;
	}

	// kiem tra xem slot co trong hay khong
	public bool isSlotEmpty(Vector2 positionGrid){
		int length = arrayRealLength ();
		for (int i=0; i<length; i++) {
			Vector2 oldPos = ballPositionList[i];
			if(oldPos.x == positionGrid.x && oldPos.y == positionGrid.y ) return false;
		}
		return true;
	}

	// tra ve toa do thuc te tu toa do luoi (grid pos)
	public Vector3 vector3FromGridPos(int xGrid, int yGrid){
		float x = xGrid * (Define.widthOfGridSquare + Define.widthBetweenTwoSquare);
		float y = yGrid * (Define.heightOfGridSquare + Define.heightBetweenTwoSquare);
		return new Vector2 (x, y);
	}
}